/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import blueprint4j.gui.db.EntityEditor;
import blueprint4j.utils.BindException;
import blueprint4j.utils.BindFieldInterface;
import blueprint4j.utils.Bindable;

public abstract class DataPanel extends JScrollPane implements DataPanelInterface {
	private Binder binder;
	private Bindable bindable;
	private Box box;
	public JPanel panel = null;
	private EntityEditor entity_editor = null;

	public DataPanel(){
	}
	
	public void setEntityEditor(EntityEditor entity_editor) {
		this.entity_editor = entity_editor;
	}
	
	public EntityEditor getEntityEditor() {
		return entity_editor;
	}
	
	public void setBinderBindable(Binder p_binder,Bindable p_bindable)
	//throws IOException, DataException, SQLException{
	throws IOException, BindException{
		binder=p_binder;
		bindable=p_bindable;
		panel = new JPanel(new BorderLayout());
		box = Box.createVerticalBox();
		box.setAlignmentX(Box.LEFT_ALIGNMENT);
		panel.add(box, BorderLayout.NORTH);
		setViewportView(panel);
		build(binder,bindable);
	}

	protected void add(JComponent component){
		box.add(component);
	}

	protected void addGroup(DataGroup group){
		box.add(group);
	}
	
	public abstract String getName();
	public abstract void build(Binder binder,Bindable bindable)
	throws IOException, BindException;
	
	public JComponent getComponent() {return this;}
	
	/*
	public static boolean doesPanelContain(String name,JComponent component) {
		if (component instanceof JLabel) {
			if (((JLabel)component).getText() != null && ((JLabel)component).getText().equals(name)) {
				return true;
			}
		}
		for (int i = 0;component.getComponents() != null && i < component.getComponents().length;i++) {
			if (component.getComponents()[i] instanceof JComponent && doesPanelContain(name,(JComponent)component.getComponents()[i])) {
				return true;
			}
		}
		return false;
	}

	public boolean doesPanelContain(String name) {
		return doesPanelContain(name,this);
	}*/
	public boolean doesPanelContain(BindFieldInterface field) 
	throws BindException {
		return doesPanelContain(bindable,field);
	}
	
	public static boolean doesPanelContain(Bindable bindable,BindFieldInterface field) 
	throws BindException {
		if (bindable != null) {
			for (int i =0;i < bindable.getBindFields().length;i++) {
				if (field == bindable.getBindFields()[i]) {
					return true;
				}
			}
		}
		return false;
	}
}

